package cn.edu.cdu.farmdeal.controller;

import cn.edu.cdu.farmdeal.domain.Product;
import cn.edu.cdu.farmdeal.service.ProductService;
import cn.edu.cdu.farmdeal.utils.Page;
import cn.edu.cdu.farmdeal.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

import static cn.edu.cdu.farmdeal.column.UtilColumn.pageSize;

/**
 * @Author: tupeng
 * @Date: 2020/3/16
 **/
@Controller
public class SearchController {
    @Autowired
    private ProductService productService;


    @RequestMapping("/search.html")
    public ModelAndView add(ModelAndView modelAndView,
                            HttpServletRequest request){
        HttpSession session1 = request.getSession();
//        session1.setAttribute("userid", "admin");
        modelAndView.setViewName("search");
        return modelAndView;
    }

    @PostMapping("/search")
    @ResponseBody
    public R search(@RequestParam("inputvalue") String inputvalue,
                    @RequestParam("pageNum") String pageNum){
        Page page=new Page();
        page.setPagenNum(Integer.parseInt(pageNum));
        page.setPageSize(pageSize);
        //查询出goods表中的记录数
        int count= productService.getTotalCount(inputvalue);
        page.setTotalCount(count);
        //获取此页码下的产品数据
        List<Product> productList=productService.search(inputvalue,page);
        HashMap map=new HashMap(10);
        map.put("page",page);
        map.put("product",productList);
        return R.ok("请求成功").put("data",map);
    }
}
